package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecService {
    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    /**
     * 根据商品分类id查询规格组
     * @param id
     * @return
     */
    public List<SpecGroupDTO> queryGroupByCategory(Long id) {
        //查询规格组
        SpecGroup s = new SpecGroup();
        s.setCid(id);
        List<SpecGroup> specGroups = specGroupMapper.select(s);
        if (CollectionUtils.isEmpty(specGroups)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }

        //对象转换
        List<SpecGroupDTO> specGroupDTOs = BeanHelper.copyWithCollection(specGroups,SpecGroupDTO.class);
        return specGroupDTOs;
    }

    /**
     * 根据规格id查询规格参数
     * @param gid
     * @return
     */
    public List<SpecParamDTO> queryGroupParamsByGid(Long gid) {
        //查询规格参数
        SpecParam s = new SpecParam();
        s.setGroupId(gid);
        List<SpecParam> specParams = specParamMapper.select(s);

        if (CollectionUtils.isEmpty(specParams)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }

        //对象转换
        List<SpecParamDTO> specParamDTOS = BeanHelper.copyWithCollection(specParams,SpecParamDTO.class);
        return specParamDTOS;
    }

    /**
     * 新增规格组
     * @param specGroupDTO
     */
    @Transactional
    public void saveSpecGroup(SpecGroupDTO specGroupDTO) {
        //新增规格组
        SpecGroup s = BeanHelper.copyProperties(specGroupDTO, SpecGroup.class);
        int count = specGroupMapper.insertSelective(s);
        if (count != 1){
            //新增失败
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 修改规格组
     * @param specGroupDTO
     */
    @Transactional
    public void updateSpecGroup(SpecGroupDTO specGroupDTO) {
        //修改规格组
        SpecGroup s = BeanHelper.copyProperties(specGroupDTO, SpecGroup.class);
        int count = specGroupMapper.updateByPrimaryKeySelective(s);
        if (count != 1){
            throw  new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    /**
     * 删除规格组
     * @param id
     */
    @Transactional
    public void deleteSpecGroup(Long id){
        //删除规格组
        int count = specGroupMapper.deleteSpecGroup(id);
        if (count != 1){
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }
    /**
     * 新增规格参数
     * @param specParamDTO
     */
    @Transactional
    public void saveSpecParam(SpecParamDTO specParamDTO) {
        SpecParam s = BeanHelper.copyProperties(specParamDTO, SpecParam.class);
        int count = specParamMapper.insertSelective(s);
        if (count != 1){
            //新增失败
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    /**
     * 修改规格参数
     * @param specParamDTO
     */
    @Transactional
    public void updateSpecParam(SpecParamDTO specParamDTO) {
        SpecParam s = BeanHelper.copyProperties(specParamDTO, SpecParam.class);
        int count = specParamMapper.updateByPrimaryKeySelective(s);
        if (count != 1){
            throw  new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    /**
     * 删除规格参数
     * @param id
     */
    @Transactional
    public void deleteSpecParam(Long id){
        int count = specParamMapper.deleteSpecParam(id);
        if (count != 1){
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

    /**
     * 根据规格组id、分类id 查询规格参数
     * @param gid
     * @param cid
     * @param searching
     * @return
     */
    public List<SpecParamDTO> querySecParams(Long gid, Long cid, Boolean searching) {
        //gid和cid必选一个
        if (gid==null && cid==null){
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }

        //查询条件
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        //查询
        List<SpecParam> specParams = specParamMapper.select(specParam);
        if (CollectionUtils.isEmpty(specParams)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(specParams,SpecParamDTO.class);
    }

    /**
     * 通过分类cid 查询规格参数
     * @param cid
     * @return
     */
    public List<SpecGroupDTO> querySpecsByCid(Long cid) {
        //查询规格组
        List<SpecGroupDTO> specGroupDTOS = queryGroupByCategory(cid);
        //查询分类下的所有规格参数
        List<SpecParamDTO> specParamDTOS = querySecParams(null, cid, null);
        //将规格参数按照groupId进行分组，得到每个group下的param的集合
        Map<Long, List<SpecParamDTO>> map = specParamDTOS.stream()
                .collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        //填写到SpecGroupDTO中
        for (SpecGroupDTO specGroupDTO : specGroupDTOS) {
            specGroupDTO.setParams(map.get(specGroupDTO.getId()));
        }

        return specGroupDTOS;
    }
}
